{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from lxml import etree\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://angelimg.spbeen.com/ang/10200/1'\n",
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36',\n",
    "    'Referer': 'http://angelimg.spbeen.com/'\n",
    "}\n",
    "all_links = [url]\n",
    "# img_links = []\n",
    "# all_imgs = []\n",
    "# title = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_next_links(url):\n",
    "    ## 给定一个链接，返回下一个链接\n",
    "    resp = requests.get(url, headers=headers)\n",
    "    ## 将拿到的响应解析一下，然后就得到了html对象\n",
    "    html = etree.HTML(resp.text)\n",
    "    \n",
    "    ## 提取当前图片\n",
    "    imgs = html.xpath('.//div[@id=\"content\"]/a/img/@src')\n",
    "#     img_links.extend(imgs)\n",
    "\n",
    "#     global title\n",
    "    title = html.xpath('.//div[@class=\"article\"]/h2/text()')\n",
    "    if bool(title):\n",
    "        title = title[0]\n",
    "    img_dir_path = 'images/{}'.format(title.split('...')[0])\n",
    "    print(title, imgs)\n",
    "    download_img(imgs, img_dir_path)\n",
    "    \n",
    "    ## 提取全部图片\n",
    "    all_img = html.xpath('.//img/@src')\n",
    "#     all_imgs.extend(all_img)\n",
    "    \n",
    "    \n",
    "    next_links = html.xpath('.//div[@id=\"page\"]/a/@href')\n",
    "    for i in next_links:\n",
    "        base_url = 'http://angelimg.spbeen.com'\n",
    "        next_url = base_url + i\n",
    "        if next_url not in all_links:\n",
    "            all_links.append(next_url)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "if not os.path.exists('angelimg'):\n",
    "    os.makedirs('angelimg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for img in img_links:\n",
    "    filename = img.split('/')[-1]\n",
    "    file_path = 'angelimg/' + filename\n",
    "    resp = requests.get(img, headers=headers)\n",
    "    print(resp)\n",
    "    with open(file_path, 'wb') as file:\n",
    "        file.write(resp.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "def download_img(img_urls:list, img_dir_path):\n",
    "    if not os.path.exists(img_dir_path):\n",
    "        os.makedirs(img_dir_path)\n",
    "\n",
    "    for img_url in img_urls:\n",
    "        filename = img_url.split('/')[-1]\n",
    "        file_path = img_dir_path + '/' + filename\n",
    "        try:\n",
    "            resp = requests.get(img_url, headers=headers, timeout=(10, 10))\n",
    "            print(img_url, resp)\n",
    "            with open(file_path, 'wb') as file:\n",
    "                file.write(resp.content)\n",
    "        except:\n",
    "            print('出现异常，当前{}跳过下载'.format(img_url))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/1\n",
      "孙俪最新单曲《感知成长的神奇》MV...【1】 ['http://image.angelimg.spbeen.com/00000mx00000/WFHX20XiomBDye2nguRi279596/kJf25S4UxYP5M4KYOOQp279596-rDZqGt.jpg']\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/2\n",
      "孙俪最新单曲《感知成长的神奇》MV...【2】 ['http://image.angelimg.spbeen.com/00000mx00000/WFHX20XiomBDye2nguRi279596/gFCLZwYNhrbYGRYzCdFd279596-F8T14O.jpg']\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/3\n",
      "孙俪最新单曲《感知成长的神奇》MV...【3】 ['http://image.angelimg.spbeen.com/00000mx00000/WFHX20XiomBDye2nguRi279596/29oFLJthDqrdiMB14cQP279596-PRsrPV.jpg']\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/4\n",
      "孙俪最新单曲《感知成长的神奇》MV...【4】 ['http://image.angelimg.spbeen.com/00000mx00000/WFHX20XiomBDye2nguRi279596/KMdnhYZ1b0M0YPd1Zwts279596-Mj8L20.jpg']\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/5\n",
      "孙俪最新单曲《感知成长的神奇》MV...【5】 ['http://image.angelimg.spbeen.com/00000mx00000/WFHX20XiomBDye2nguRi279596/UOh0cBkLmWJOZTCxQl8h279596-Aqj2cf.jpg']\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/6\n",
      "孙俪最新单曲《感知成长的神奇》MV...【6】 ['http://image.angelimg.spbeen.com/00000mx00000/WFHX20XiomBDye2nguRi279596/kJf25S4UxYP5M4KYOOQp279596-rDZqGt.jpg']\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/7\n",
      "孙俪最新单曲《感知成长的神奇》MV...【7】 ['http://image.angelimg.spbeen.com/00000mx00000/WFHX20XiomBDye2nguRi279596/gFCLZwYNhrbYGRYzCdFd279596-F8T14O.jpg']\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/8\n",
      "孙俪最新单曲《感知成长的神奇》MV...【8】 ['http://image.angelimg.spbeen.com/00000mx00000/WFHX20XiomBDye2nguRi279596/29oFLJthDqrdiMB14cQP279596-PRsrPV.jpg']\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/9\n",
      "孙俪最新单曲《感知成长的神奇》MV...【9】 ['http://image.angelimg.spbeen.com/00000mx00000/WFHX20XiomBDye2nguRi279596/KMdnhYZ1b0M0YPd1Zwts279596-Mj8L20.jpg']\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/10\n",
      "孙俪最新单曲《感知成长的神奇》MV...【10】 ['http://image.angelimg.spbeen.com/00000mx00000/WFHX20XiomBDye2nguRi279596/UOh0cBkLmWJOZTCxQl8h279596-Aqj2cf.jpg']\n",
      "当前列表的链接内容为： ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']\n",
      "当前图片链接的列表内容为： []\n",
      "当前全部图片的列表内容为： []\n",
      "当前标签的列表内容为： \n"
     ]
    }
   ],
   "source": [
    "for link in all_links:\n",
    "    print('当前请求的链接是：', link)\n",
    "    get_next_links(link)\n",
    "#     time.sleep(1)\n",
    "print('当前列表的链接内容为：', [i.split('/')[-1] for i in all_links])\n",
    "# print('当前图片链接的列表内容为：',img_links)\n",
    "# print('当前全部图片的列表内容为：',all_imgs)\n",
    "# print('当前标签的列表内容为：',title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
